/*
 * tx_functions.c
 * Created on November 29, 2005; 2:38 PM
 * by vramadurai
 */

/************ System Header Files ***************/
#include <stdio.h>
#include <stdlib.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "iqdma_3500.h"
#include "icrdefs_3500.h"

void wait_sub_buffer_filled(){
	hs_uint32_t	gifr;
	hs_uint32_t	bit;
	hs_uint32_t core_base = ICR_ADDRESS_CORE(TX_CORE);


	/*do{
		gifr = __sb_cfsr(MACH_GIFR);
		bit	= gifr>>MMIO_SB3500PSDA_INT;
	} while(!(bit&0x1));*/


	do{
		bit = ICR_GET(core_base, TX_PSD_STATUS) >> 1;
	} while(!bit);
	//printf("enter wait buffer\n");
}

void reset_sub_buffer_interrupt()
{
	/** Core 1, PSD B **/
	hs_uint32_t core_base = ICR_ADDRESS_CORE(TX_CORE);
	hs_uint32_t	gifr_mask;
	volatile unsigned* status;

	gifr_mask = 1<<MMIO_SB3500PSDA_INT;
	status = (volatile unsigned*)(core_base | MMIO_SB3500PSDA_STATUS);
	__sb_ctsr(gifr_mask, MACH_GIFR0);
    status[0] = 0x2;//enable D2A
	//ICR_SET(core_base, TX_PSD_STATUS, 0x2);
}

void capture_all_iqdata_tx_samps(int samps)
{
    FILE *fp_i;
    int i;

    fp_i = fopen("TxOutput.txt", "w");

    for(i=0; i<samps; i++){
        fprintf(fp_i, "%d %d\n", DacOutI[i], DacOutQ[i]);
    }

    fclose(fp_i);
}

void capture_all_iqdata_tx()
{
    FILE *fp_i;
    int i;

    fp_i = fopen("TxOutput.txt", "w");

    for(i=0; i<GLOBAL_BUF_SIZE_TX; i++){
        //fprintf(fp_i, "%d %d\n", IQ_SCALE(DacOutI[i]), IQ_SCALE(DacOutQ[i]));
        fprintf(fp_i, "%d %d\n", ((unsigned short)(DacOutI[i]) >> 6) - 512,
		((unsigned short)(DacOutQ[i]) >> 6) - 512);
    }

    fclose(fp_i);
}

